import rxAjax from '../../assets/js/ajax.js';

//表单权限配置 api接口
export const LoginApi = {};

LoginApi.authUrl= '/api-uaa';
LoginApi.userUrl= '/api-user';
LoginApi.systemUrl= '/api-system';



//注册微信公众号用户比登录
LoginApi.addWxGzhUserAndLogin= function(parameter) {
    return rxAjax.postJson(LoginApi.authUrl+'/oauth/user/addWxGzhUserAndLogin',parameter);
}

//获取手机验证码(注册用户)
LoginApi.getNewUserSmsCaptcha = ({mobile}) => rxAjax.get(
    LoginApi.authUrl+'/validata/smsCodeFromNewUser/'+mobile
)

//获取手机验证码
LoginApi.getSmsCaptcha = ({mobile}) => rxAjax.get(
    LoginApi.authUrl+'/validata/smsCode/'+mobile
)

//轮询微信公众是否扫码
LoginApi.getTokenByTicket = (params) => rxAjax.get(
    LoginApi.authUrl+"/oauth/user/getTokenByTicket?tenantId="+params.tenantId+"&ticket="+params.ticket+"&sceneId="+params.sceneId+"&hostName="+params.hostName
)

//登录
LoginApi.login = (parameter) =>{
    let url=LoginApi.authUrl + '/oauth/user/token';
    if(parameter.appAlias){
        url = LoginApi.authUrl + '/oauth/user/appLogin';
    }
    return rxAjax.postForm(url,parameter,{token:false})
}

//获取验证码
LoginApi.imgcode = (uuid) => rxAjax.getBlob(
    LoginApi.authUrl+'/validata/code/'+uuid
)

/**
 * 获取登录的参数配置，包括验证码，应用名称，是否需要输入租户机构
 */
LoginApi.getLoginConfig = function (hostName) {
    let url = '/api-system/system/core/public/getLoginConfig?hostName='+hostName;
    return rxAjax.get(url);
}

//获取用户信息
LoginApi.getUserInfo = () => rxAjax.get(
    LoginApi.authUrl +'/user/org/osUser/current'
)

//退出登录
LoginApi.logout = (token) => rxAjax.postUrl(
    LoginApi.authUrl+'/oauth/remove/token',
    {"token": token}
)

//用户首次登录修改密码
LoginApi.changePassword = (parameter) => rxAjax.postForm(
    LoginApi.userUrl + '/user/org/osUser/changePassword',
    parameter
)

//获取后端用户与路由菜单信息
LoginApi.getLoginUserAndMenu = function () {
    return rxAjax.get("/api-user/user/org/osUser/getLoginUserAndMenu");
}

//根据微信code自动登录code,tenantId,loginType
LoginApi.autoLoginByCode=(params)=>rxAjax.postUrl(
    LoginApi.authUrl+'/oauth/user/autoLogin',params
)

//获取第三方平台授权地址参数tenantId,loginType,redirectUrl, state
LoginApi.getAuthUrl = (params) => rxAjax.get(
    LoginApi.authUrl+"/oauth/user/authUrl?tenantId="+params.tenantId+"&loginType="+params.loginType+"&redirectUrl="+params.redirectUrl+"&state="+params.state
)

/**/
LoginApi.getAllUserTypeList = function (parameter){
    return rxAjax.get('/api-user/user/org/osUserType/getAllByTenantId',parameter).then (res => {
        return res.data
    })
}

LoginApi.edit = function (pkId,groupId,tenantId) {
    let ary = [], url= '/api-user/user/org/osUser/edit';
    if(pkId){
        ary.push("pkId=" + pkId);
    }
    if(groupId){
        ary.push("groupId=" + groupId);
    }
    if(tenantId){
        ary.push("tenantId=" + tenantId);
    }
    if(ary.length>0){
        url+="?" + ary.join("&");
    }
    return rxAjax.get(url);
}

//获取所有的扩展属性
LoginApi.getAllProperties=function (parameter) {
    let url=  '/api-user/user/org/osPropertiesGroup/getAllProperties';
    return rxAjax.postForm(url,parameter);
}

LoginApi.saveOsUser= function(parameter) {
    return rxAjax.postJson('/api-user/user/org/osUser/save',parameter);
}

LoginApi.getByAlias =function(alias){
    let url = '/api-system/system/core/sysDic/getByAlias?alias='+alias;
    return rxAjax.get(url);
}

LoginApi.updatePassword = function (parameter) {
    return rxAjax.postUrl('/api-user/user/org/osUser/updatePassword',parameter);
}

LoginApi.setPassword = function (parameter) {
    return rxAjax.postUrl('/api-user/user/org/osUser/setPassword',parameter);
}
//绑定第三方平台，参数code,tenantId,loginType
LoginApi.platformBind=function(params) {
    let url= "/api-uaa/oauth/user/bind";
    return rxAjax.postForm(url,params).then (res => {
        return res
    });
}

//根据code自动登陆,参数code,tenantId,loginType
LoginApi.platformAutoLogin=function(params) {
    let url= "/api-uaa/oauth/user/autoLogin";
    return rxAjax.postForm(url,params).then (res => {
        return res
    });
}
export  default LoginApi;
